﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using iLearn.Models;
namespace iLearn.Controllers
{
    public class StudentController : Controller
    {
        //
        // GET: /Student/
        private iLearnDBDataContext iDB = new iLearnDBDataContext();
        public ActionResult Index()
        {
            var std = from s in iDB.Students
                      select s;
            return View(std);
        }

        //
        // GET: /Student/Details/5

        public ActionResult Details(int id)
        {
            var std = from s in iDB.Students
                          where s.id == id
                          select s;

            return View(std.SingleOrDefault());
        }

        //
        // GET: /Student/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Student/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            Student std = new Student();
            std.last_name = collection["last_name"];
            std.first_name = collection["first_name"];
            std.dob = Convert.ToDateTime(collection["dob"]);
            std.user_name = collection["user_name"];
            std.password = collection["password"];
            std.address = collection["address"];
            try
            {
                // TODO: Add insert logic here
                iDB.Students.InsertOnSubmit(std);
                iDB.SubmitChanges();
               
                return RedirectToAction("Index");
            }
            catch
            {
                return View(std);
            }
        }
        
        //
        // GET: /Student/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Student/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Student/Delete/5
 
        public ActionResult Delete(int id)
        {
            var std = from s in iDB.Students
                      where s.id == id
                      select s;
            Student deletestudent = std.SingleOrDefault();
            return View(deletestudent);
        }

        //
        // POST: /Student/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            // TODO: Add delete logic here
            var std = from s in iDB.Students
                      where s.id == id
                      select s;
            Student deletestudent = std.SingleOrDefault();
            try
            {
               
                iDB.Students.DeleteOnSubmit(deletestudent);
                iDB.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(deletestudent);
            }
        }
    }
}
